package com.yami.shop.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.yami.shop.bean.app.dto.UserCollectionDto;
import com.yami.shop.bean.event.EsSearchProdEvent;
import com.yami.shop.bean.model.ProdLang;
import com.yami.shop.bean.model.UserCollection;
import com.yami.shop.bean.param.EsProductParam;
import com.yami.shop.bean.vo.search.EsProductSearchVO;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.dao.UserCollectionMapper;
import com.yami.shop.manager.impl.LangManager;
import com.yami.shop.service.UserCollectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 用户收藏表
 *
 * @author xwc
 * @date 2019-04-19 16:57:20
 */
@Service
public class UserCollectionServiceImpl extends ServiceImpl<UserCollectionMapper, UserCollection> implements UserCollectionService {

    @Autowired
    private UserCollectionMapper userCollectionMapper;
    @Autowired
    private ApplicationContext applicationContext;
    @Autowired
    private LangManager langManager;

    @Override
    public IPage<UserCollectionDto> getUserCollectionDtoPageByUserId(Page page, String userId, String prodName, Integer prodType) {
        IPage<UserCollectionDto> userCollectionPage = userCollectionMapper.getUserCollectionDtoPageByUserId(page, userId, prodName, prodType);
        if (CollUtil.isEmpty(userCollectionPage.getRecords())) {
            return userCollectionPage;
        }
        List<Long> ids = userCollectionPage.getRecords().stream().filter(item -> Objects.nonNull(item.getProdId())).map(UserCollectionDto::getProdId).collect(Collectors.toList());
        Map<Long, ProdLang> prodLangMap = langManager.getProdLangMap(ids);
        for (UserCollectionDto item : userCollectionPage.getRecords()) {
            ProdLang prodLang = prodLangMap.get(item.getProdId());
            if (Objects.nonNull(prodLang)) {
                item.setProdName(prodLang.getProdName());
            }
        }
        return userCollectionPage;
    }

    @Override
    public void orderProdCollectionAll(List<Long> prodIdList, String userId) {
        List<UserCollection> userCollectionListDb = userCollectionMapper.selectList(new LambdaQueryWrapper<UserCollection>()
                .eq(UserCollection::getUserId, userId)
                .in(UserCollection::getProdId, prodIdList));
        for (UserCollection userCollection:userCollectionListDb){
            prodIdList.remove(userCollection.getProdId());
        }
        if (CollUtil.isEmpty(prodIdList)) {
            return;
        }
        List<UserCollection> userCollectionList = Lists.newArrayList();
        for (Long prodId:prodIdList) {
            UserCollection userCollection = new UserCollection();
            userCollection.setUserId(userId);
            userCollection.setCreateTime(new Date());
            userCollection.setProdId(prodId);
            userCollectionList.add(userCollection);
        }
        saveBatch(userCollectionList);
    }

    @Override
    public IPage<EsProductSearchVO> collectionProds(PageParam page, String userId) {
        IPage<UserCollection> userCollectionList = this.page(page, new LambdaQueryWrapper<UserCollection>().eq(UserCollection::getUserId, userId).orderByDesc(UserCollection::getCreateTime));
        List<Long> prodIds = userCollectionList.getRecords().stream().map(UserCollection::getProdId).collect(Collectors.toList());
        if (CollUtil.isEmpty(prodIds)) {
            return new PageParam<>();
        }
        EsProductParam productParam = new EsProductParam();
        productParam.setProdIds(prodIds);
        productParam.setAppDisplay(false);
        EsSearchProdEvent esSearchProdEvent = new EsSearchProdEvent();
        esSearchProdEvent.setProductParam(productParam);
        esSearchProdEvent.setPageParam(page);
        applicationContext.publishEvent(esSearchProdEvent);
        return esSearchProdEvent.getProductSearchPage();
    }
}
